package com.example.jinmingwu.a180503_datepickerdialog;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.view.View.OnClickListener;
import android.app.DatePickerDialog;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private TextView showdate;
    private TextView nowtime;
    private Button setdate;
    private int year;
    private int month;
    private int day;

//    private int Nyear;
//    private int Nmonth;
//    private int Nday;

    Calendar current_calendar= Calendar.getInstance(Locale.CHINA);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showdate = (TextView) this.findViewById(R.id.showtime);
        setdate = (Button) this.findViewById(R.id.setdate);
        nowtime=(TextView) this.findViewById(R.id.nowtime);

        //初始化Calender日历对象
        Calendar c = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date();

        c.setTime(mydate);
        current_calendar.setTime((mydate));
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
//        Nyear=year;
//        Nmonth=month;
//        Nday=day;
        showdate.setText("设置的日期：" + year + "-" + (month + 1) + "-" + day);
        nowtime.setText("现在系统日期："+current_calendar.getTime());

        setdate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog my_datePickerDialog = new DatePickerDialog(MainActivity.this, DateListener , year, month, day);
                my_datePickerDialog.show();
            }
        });
    }

    private DatePickerDialog.OnDateSetListener DateListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker v, int y, int m, int d) {
            //方法一：
//            if(y>Nyear||(y==Nyear&&m>Nmonth)||(y==Nyear&&m==Nmonth&&d>Nday)){
//                Toast.makeText(getApplicationContext(),"选择错误！", Toast.LENGTH_SHORT).show();
//            }
//            else{
//                year=y;
//                month=m;
//                day=d;
//                updateDate();
//            }
            //if (c.getTime()<= current_date.getTime().toString()) {
//            if (c.getTime()<=current_date.getTime()) {
//                year = y;
//                month=m;
//                day=d;
//                updateDate();
//            }
            //方法二：
//            Calendar calendar = Calendar.getInstance(Locale.CHINA);
//            calendar.set(y,m,d);
////            showdate.setText(current_calendar.getTime().toString()+" "+c.getTime().toString());
//
////            Date date=new Date(y,m,d);
////            Date current_date = new Date(System.currentTimeMillis());
////            showdate.setText(Long.toString(current_date.getTime())+" "+Long.toString(date.getTime()));
//            if(((current_calendar.getTime()).compareTo(calendar.getTime()))>=0){
//                year = y;
//                month=m;
//                day=d;
//                updateDate();
//            }
//            else{
//                Toast.makeText(getApplicationContext(),"选择错误！不能选择大于当前时间", Toast.LENGTH_SHORT).show();
//            }
            //方法三：
            Calendar Now_calendar=Calendar.getInstance(Locale.CHINA);
            Date current_date=Now_calendar.getTime();
            Calendar setCalendar=Calendar.getInstance();
            setCalendar.set(y,m,d);
            Date set_date=setCalendar.getTime();
            if(current_date.getTime()>=set_date.getTime()){
                year=y;
                month=m;
                day=d;
                updateDate();
            }
            else{
                Toast.makeText(getApplicationContext(),"选择错误！不能选择大于当前时间",Toast.LENGTH_SHORT).show();
            }
        }
    };
    private void updateDate(){
        showdate.setText("设置的日期：" + year + "-" + (month + 1) + "-" + day);
    }
}





